package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.ConditionAdapter;
import com.kkd.kkdapp.adapter.MyRegularAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.ConditionData;
import com.kkd.kkdapp.http.entity.PoiList;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;
import com.kkd.kkdapp.view.ScrollerLayout.ScrollStateProvider;

/**
 * 投标状况
 * 
 * @author mty
 * 
 */
public class ConditionActivity extends BaseActivity implements OnClickListener,
		KKDScrollListener {

	private final int CODE_ZERO = 1; // code=0
	private final int CODE_ELSE = 2; // code=其它

	private List<PoiList> poilist = new ArrayList<PoiList>();
	private KkdXlistView condition_lv; // 列表ListView
	private RelativeLayout condition_img;
	private String bidId;
	private ImageView img_null;
	private TextView tv_null;
	private String desc;
	private int pageNO = 1;
	private int pageSize = 10;
	private boolean REFRESHFLAG = true;
	private int mPageNo;
	private int mPageCount;
	private ConditionAdapter adapter;
	private ConditionData data;

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO: // code=0
				if (adapter == null) {
					adapter = new ConditionAdapter(ConditionActivity.this,
							poilist);
					mListView.setAdapter(adapter);
				}
				if (mPageNo > mPageCount) {
					condition_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						condition_lv.stopLoadMore(1);
					}
				} else {
					condition_lv.setNoMore(false);
					if (pageNO == 1) {
						poilist.clear();
					}
					if(data.getPoiList()==null)
						return;
					poilist.addAll(data.getPoiList());
					adapter.notifyDataSetChanged();
					
					if (data.getPoiList().size() < pageSize) {
						condition_lv.stopLoadMore(1);
					} else {
						condition_lv.stopLoadMore(0);
					}
				}
				condition_lv.stopRefresh();
				condition_lv.initState();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				condition_lv.setRefreshTime(time);
				LogUtil.e("poilist.size()", poilist.size() + "");
				if (REFRESHFLAG) {
					if (poilist.size() == 0) {
						img_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
						tv_null.setText("暂无记录");
						img_null.setImageResource(R.drawable.img_null);
					} else {
						tv_null.setVisibility(View.GONE);
						img_null.setVisibility(View.GONE);
					}
				}
				break;
			case CODE_ELSE: // code=其它
				condition_lv.stopLoadMore(1);
				condition_lv.stopRefresh();
				break;
			default:
				break;
			}
		};
	};
	private ListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_condition);
		initView();
		setListener();

	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		condition_lv = (KkdXlistView) this.findViewById(R.id.condition_lv);
		condition_img = (RelativeLayout) this.findViewById(R.id.condition_img);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);
		mListView = condition_lv.mListView;
		mListView.setDividerHeight(0);
		getMessage();
		condition_lv.startRefresh();
		if (AppUtil.isConnect(this)) {
			requestCondition();
		} else {
			// mXListView.setPullLoadEnable(false);
			img_null.setVisibility(View.VISIBLE);
			tv_null.setVisibility(View.VISIBLE);
			tv_null.setText("网络连接失败，请检查网络！");
			img_null.setImageResource(R.drawable.img_null);
			condition_lv.stopRefresh();
		}
	}

	public void getMessage() {
		Intent intent = this.getIntent();
		bidId = intent.getStringExtra("bidId");
	}

	public void setListener() {
		condition_img.setOnClickListener(this);
		condition_lv.setKKDScrollListener(this);
		// condition_lv.setXListViewListener(this);
		// condition_lv.setPullRefreshEnable(true);
		// condition_lv.setPullLoadEnable(false);
		// condition_lv.setScrollStateProvider(new ScrollStateProvider() {
		//
		// @Override
		// public boolean canPullUp() {
		// return false;
		// }
		//
		// @Override
		// public boolean canPullDown() {
		// boolean isPull = false;
		// if (mListView.getFirstVisiblePosition() == 0) {
		// final View topChildView = mListView.getChildAt(0);
		// if (topChildView != null)
		// isPull = topChildView.getTop() >= 0;
		// else
		// isPull = true;
		// }
		// return isPull;
		// }
		// });
	}

	public void requestCondition() {
		LogUtil.e("bidId", bidId);
		KkdHttpMagager.getInstance().getConditionManager(pageNO, pageSize,
				bidId, ConditionData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (ConditionData) responseBody;
						mPageNo = data.getPageNo();
						mPageCount = data.getPageCount();
						mHandler.sendEmptyMessage(CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = CODE_ELSE;
						msg.obj = desc;
						mHandler.sendMessage(msg);
					}
				});
	}

	public void setadapter(List<PoiList> poilist) {

		// adapter = new ConditionAdapter(this);
		// adapter.appendData(poilist, true);
		// adapter.notifyDataSetChanged();
		// condition_lv.stopRefresh();
		// Time_Date time_Date = new Time_Date();
		// String time = time_Date.get_Year_Day();
		// condition_lv.setRefreshTime(time);
		// mListView.setAdapter(adapter);
		// condition_lv.startRefresh();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.condition_img:
			finish();
			break;
		}

	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		requestCondition();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestCondition();
	}

}
